package com.cci.kangdao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.service.FaultService;
import com.cci.kangdao.service.impl.SMSEnum;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.NumberUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: shiliqiang
 * @company: CTY Technology Co.,Ltd
 * @since: 2021/7/22 10:11
 * @version: V1.0
 */
@RestController
@RequestMapping("/fault/")
public class FaultController {
    private Logger log = Logger.getLogger(this.getClass().getName());
    @Autowired
    private FaultService faultService;


     /**
      * @description: 查询照片
      * @param:
      * @return: 
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2021/7/22 10:14
      * @version: V1.0
      */
    @RequestMapping(value = "getPictures.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    //@ResponseBody
    public void getPictures(@RequestBody String str, HttpServletResponse response) {
        log.info("接口传参为"+str);
        Map map = JSON.parseObject(str);
        Long orderId = MapUtils.getLong(map, "orderId", 0L);
        // 查询照片的位置  1-维修前；2-维修中；
        Integer position = MapUtils.getInteger(map,"position");
        if (position == null) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        if (!NumberUtils.isPositiveNum(orderId)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        Map<String, Object> data = new HashMap<>(1);
        data.put("pictureList", faultService.getPictureList(map));

        ParameterTool.writeSuccessResponse(response, data);
    }


     /**
      * @description: 判断必填照片是否已填
      * @param:
      * @return: 
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2021/7/22 16:30
      * @version: V1.0
      */
    @RequestMapping(value = "pictureUpload/isFinish.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    //@ResponseBody
    public void isFinish(@RequestBody String str, HttpServletResponse response) {
        log.info("接口传参为"+str);
        Map map = JSON.parseObject(str);
        Long orderId = MapUtils.getLong(map, "orderId", 0L);
        // 查询照片的位置  1-维修前；2-维修中；
        Integer position = MapUtils.getInteger(map,"position");
        if (position == null) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        if (!NumberUtils.isPositiveNum(orderId)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        ParameterTool.writeSuccessResponse(response, faultService.repairIsFinish(map));
    }

     /**
      * @description: 删除故障照片
      * @param:
      * @return:
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2021/7/23 13:41
      * @version: V1.0
      */
    @RequestMapping(value = "faultPicture/delete.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public void delete(@RequestBody String str, HttpServletResponse response) {
        log.info("接口传参为"+str);
        Map map = JSON.parseObject(str);
        Long orderId = MapUtils.getLong(map, "orderId");
        String faultId = MapUtils.getString(map,"faultId");
        if (StringUtils.isEmpty(faultId)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        if (!NumberUtils.isPositiveNum(orderId)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        Map<String, Object> data = new HashMap<>(1);
        int updatePicture = faultService.deleteFaultPicture("-"+faultId, orderId, -1);
        int result = 1;
        if (updatePicture < 0) {
            result = -1;
        }
        // 删除故障信息
        Map<String, Object> deleMap = new HashMap<>();
        deleMap.put("flag", -1);
        deleMap.put("faultId", faultId);
        deleMap.put("orderId", orderId);
        deleMap.put("updator", UserThreadLocal.get().getId());
        deleMap.put("updateTime", LocalTimeUtil.getRealCurrentTime());
        //int updateFault = faultService.updateFault(faultId, orderId, -1);
        int updateFault = faultService.updateFault(deleMap);
        if (updateFault < 0) {
            result = -1;
        }
        data.put("isDelete", result);
        ParameterTool.writeSuccessResponse(response, data);

    }

     /**
      * @description: 新增故障信息
      * @param:
      * @return:
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2021/7/23 15:53
      * @version: V1.0
      */
    @RequestMapping(value = "faultInfo/save.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public void save(@RequestBody String str, HttpServletResponse response) {
        log.info("接口传参为"+str);
        Map map = JSON.parseObject(str);
        List<JSONObject> list = (List<JSONObject>) map.get("faults");
        if (CollectionUtils.isEmpty(list)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        Long orderId = MapUtils.getLong(map, "orderId");
        if (!NumberUtils.isPositiveNum(orderId)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        Map<String, Object> data = new HashMap<>(1);
        data.put("isSave", faultService.saveFaultInfo(list, orderId));
        ParameterTool.writeSuccessResponse(response, data);
    }

     /**
      * @description: 查询故障信息
      * @param:
      * @return: 
      * @exception:
      * @author: shiliqiang
      * @company: CTY Technology Co.,Ltd
      * @since: 2021/7/23 16:34
      * @version: V1.0
      */
    @RequestMapping(value = "getFaultInfo.aspx", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public void getFaultInfo(@RequestBody String str, HttpServletResponse response) {
        log.info("接口传参为"+str);
        Map map = JSON.parseObject(str);
        Long orderId = MapUtils.getLong(map, "orderId");
        if (!NumberUtils.isPositiveNum(orderId)) {
            ParameterTool.writeResponse(response, SMSEnum.type17.getIndex(), SMSEnum.type17.getName());
        }
        ParameterTool.writeSuccessResponse(response, faultService.getFaultInfo(orderId));
    }
}
